package calculator;



import calculator.handler.ExpressionHandler;
import calculator.operator.Operator;

import java.util.List;
import java.util.Stack;

public class Calculator {

    public double calculate(List<Object> list){

        Stack<Double> numStack = new Stack<>();

        while (!list.isEmpty()){

            if (isNumber(list.get(0))){
                numStack.push((double)list.remove(0));
            }else {
                numStack.push(((Operator)list.remove(0)).doCal(numStack));
            }

        }
        //最终结果
        return numStack.pop();
    }

    public double calculate(String expr){
        ExpressionHandler handler = new ExpressionHandler();
        return calculate(handler.toSuffix(expr));
    }

    private boolean isNumber(Object o){
        return o instanceof Double;
    }

}
